import 'package:flutter/material.dart';
import 'package:flutter_base/bean/knowledge/knowlege_entity.dart';
import 'package:flutter_base/page/knowleage/detail/knowledge_tab_child_page.dart';
import 'package:flutter_base/viewmodel/knowledge/knowledge_detail_vm.dart';
import 'package:provider/provider.dart';

///
/// @DIR_PATH：lib/bean/knowledge/detail
/// @TIME：2024/6/13 14:47
/// @AUTHOR：starr

class KnowledgeDetailPage extends StatefulWidget{

  const KnowledgeDetailPage({super.key, required this.tabList});

  final List<KnowlegeDataChildren>? tabList;
  @override
  State<StatefulWidget> createState()  => _KnowledgeDetailState();

}

class _KnowledgeDetailState extends State<KnowledgeDetailPage> with SingleTickerProviderStateMixin{

  late TabController tabController;
  KnowledgeDetailViewModel viewModel =KnowledgeDetailViewModel();

  @override
  void initState() {
    super.initState();
    tabController =TabController(length: widget.tabList?.length ?? 0, vsync: this);
    viewModel.initTabs(widget.tabList);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(create: (context){
      return viewModel;
    },child: Scaffold(
      appBar: AppBar(
        title: TabBar(
          tabs: viewModel.tabs,
          controller: tabController,
          labelColor: Colors.blue,
          indicatorColor: Colors.blue,
          isScrollable: true,
        ),
      ),
      body: SafeArea(
        child: TabBarView(
          controller: tabController,
          children: tabChildren(),
        ),
      ),
    ),);
  }

  List<Widget> tabChildren(){
    return widget.tabList?.map((e) => KnowledgeTabChildPage(treeId : e.id)).toList() ?? [];
  }

}
